import Vue from 'vue';
import axios from "axios";
/**
 * @description 存储localstorage
 * @param {存储的keyName} name
 * @param {存储的值} value
 */
export const setStore = (name, value) => {
  if (!name) return;
  if (typeof value !== 'string') {
    value = JSON.stringify(value)
  }
  window.sessionStorage.setItem(name, value)
}

/**
 * @description 读取localstorage
 * @param {存储的keyName} name
 */
export const getStore = (name) => {
  if (!name) return;
  return window.sessionStorage.getItem(name)
}

/**
 * @description 删除localstorage
 * @param {需要删除的keyName} name
 */
export const removeStroe = (name) => {
  if (!name) return;
  window.sessionStorage.removeItem(name)
}
/**
 * @description 按日处理时间为xxxx-yy-dd
 * @param {需要处理的时间} time:object
 */
export const timeProcessDay = (time) => {
    if(!time){return ""}
  let d = new Date(time);
  return d.getFullYear() + ((d.getMonth() + 1) >= 10 ? ('-' + (d.getMonth() + 1)) : ('-0' + (d.getMonth() + 1))) + "-" + (d.getDate() >= 10 ? d.getDate() : '0' + d.getDate());

}
/**
 * @description 按月处理时间为xxxx-yy
 * @param {需要处理的时间} time:object
 */
export const timeProcessMonth = (time) => {
  let d = new Date(time);
  return d.getFullYear() + ((d.getMonth() + 1) >= 10 ? ('-' + (d.getMonth() + 1)) : ('-0' + (d.getMonth() + 1)));

}
/**
 * @description 按月处理时间为yyyyMM
 * @param {需要处理的时间} time:object
 */
export const timeProcessMonth1 = (time) => {
  let d = new Date(time);
  return d.getFullYear() + ((d.getMonth() + 1) >= 10 ? (d.getMonth() + 1) : ('0' + (d.getMonth() + 1)));

}
export const getUrlKey = function (name) {
  return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ""])[1].replace(/\+/g, '%20')) || null;
}


/**
 * @description 处理翻页数据
 * @param {返回的翻页数据} responsePage:object
 * @param {请求的翻页数据} requestPage:object
 */
export const pageFormat = function (responsePage, requestPage, totalKey, currentKey, pageSizeKey, pageShowKey) {
  requestPage[pageShowKey] = true;
  requestPage[currentKey] = responsePage.currentPage;
  requestPage[pageSizeKey] = responsePage.pageSize;
  requestPage[totalKey] = responsePage.totalRecords;
  if (responsePage.currentPage > responsePage.totalPage || responsePage.totalRecords <= responsePage.pageSize) {
    requestPage[pageShowKey] = false;
  }
  Vue.set(requestPage, totalKey, responsePage.totalRecords);
}

export const pageFormatAssign = function (responsePage, requestPage) {
  let pageShow = true;
  Object.assign(requestPage,responsePage)
  if (responsePage.currentPage > responsePage.totalPage || responsePage.totalRecords <= responsePage.pageSize) {
    pageShow = false;
  }
  Vue.set(requestPage, 'pageShow', pageShow);
}

export class responsePage {
  currentPage;
  totalPage;
  pageSize;
  totalRecords;
}

export const formatDept = function (obj,type,from) {
  if (type == 'firstDept') {
    if (!obj.firstDeptId  || obj.firstDeptId =='') {
      obj.firstDeptId = obj.deptId;
      obj.firstDeptName = obj.deptName;
    }
  }
  if (type == 'dept') {
    if (!obj.deptName || obj.deptName =='') {
      obj.deptId = obj.firstDeptId;
      obj.deptName = obj.firstDeptName;
      /*obj.firstDeptId = '';
      obj.firstDeptName = '';*/
    }
  }
  if (type == 'assign') {
    obj.deptName = from.deptName;
    obj.deptId = from.deptId;
    obj.firstDeptId = from.superDeptId;
    obj.firstDeptName = from.superDeptName;
  }
  if (type == 'clear') {
    Object.assign(obj,{
      deptId:null,
      deptName:null,
      firstDeptId:null,
      firstDeptName:null
    })
  }
}
